/**
 * Version 14 mrt 2008 - initial version
 */
package audioreaper.machine;


/**
 * <code>DefaultMachineContext</code> is the default audio context,
 * which is 16 bit, 44.1kHz stereo. This has no connection with the actual
 * audio-card capabilities.
 *
 * @author <a href="mailto:vincent@nazarene.nl">Vincent van Beveren</a>
 *
 */
public class DefaultMachineContext implements MachineContext {

	private static final int BITS_PER_SAMPLE = 16;
	private static final int SAMPLE_RATE = 44100;
	private static final int BUFFER_SIZE = 1024 * 2;
	
	private Master _master;
	
	public DefaultMachineContext() {
		_master = new Master(this);
	}
	
	public Master getMaster() {
		return _master;		
	}
	
	/* (non-Javadoc)
	 * @see vanbeveren.audioreaper.model.AudioContext#createBuffer()
	 */
	@Override
	public int[] createBuffer() {
		return new int[getBufferSize()];
	}

	@Override
	public int getBitsPerSample() {
		return BITS_PER_SAMPLE;
	}

	@Override
	public int getBufferSize() {
		return BUFFER_SIZE;
	}

	/* (non-Javadoc)
	 * @see vanbeveren.audioreaper.model.AudioContext#getChannelConfig()
	 */
	@Override
	public ChannelConfig getChannelConfig() {
		return ChannelConfig.STEREO;
	}

	/* (non-Javadoc)
	 * @see vanbeveren.audioreaper.model.AudioContext#getNode(java.lang.String)
	 */
	@Override
	public Machine getNode(String name) {
		if (_master == null) {
			return null;
		}
		if (_master.getName().equals(name)) {
			return _master;
		}
		return getNode(_master, name);
	}
	
	private Machine getNode(Target t, String name) {
		for (Machine node : t.getSourceList()) {
			if (node.getName().equals(name)) {
				return node;
			}
			if (node instanceof Target) {
				Machine n = getNode((Target)node, name);
				if (n != null) {
					return n;
				}
			}
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see vanbeveren.audioreaper.model.AudioContext#getSampleRate()
	 */
	@Override
	public int getSampleRate() {
		return SAMPLE_RATE;
	}

}
